using System;
using System.Collections.Generic;

namespace VisionarySystems.TimeRegi.Model.TimePeriods
{
    public class WorkWeek : WorkDayHost<DateTime>, ITimePeriode
    {
        #region Private members

	    readonly int m_WeekNo;
        int m_Year;

        #endregion

        #region Ctor

        public WorkWeek(int WeekNo, int Year)
        {
            m_WeekNo = WeekNo;
            m_Year = Year;
            if ((m_WeekNo < 1) || (m_WeekNo > 53))
                throw new ArgumentOutOfRangeException("WeekNo must be between 1 and 53");
            if ((m_Year < 2011) || (m_Year > 2020))
                throw new ArgumentOutOfRangeException("Year must be between 2011 and 2020");
			ID = new TimePeriodeID(GetUniqueIndex(), TimePeriodeType.WorkWeek);

        }

        #endregion

        #region Properties

        public int Year
        {
            get { return m_Year; }
        }

		public int WeekNo
		{
			get { return m_WeekNo; }
		}


        #endregion

        #region Public Methods

        public static string GetWeek(int WeekNo)
        {
            return string.Format("Uge {0:D2}", WeekNo);
        }

        public override string ToString()
        {
            return string.Format("{0} ({1})", GetWeek(m_WeekNo), GetHours(WorkedTime));
        }

        public TimeSpan BrakeTime { get; set; }

        public void Add(IEnumerable<TimeSlot> TimeSlots)
        {
            foreach (TimeSlot slot in TimeSlots)
            {
                ITimePeriode currentDate;
                if (!m_TimePeriods.TryGetValue(slot.Date, out currentDate))
                {
                    currentDate = new WorkDay(slot.Date);
                    m_TimePeriods.Add(slot.Date, currentDate);
					//currentDate.ID = GetNextID(currentDate);
                }
                currentDate.Add(slot);
            }
        }

		public IEnumerable<TimeSlot> TimeSlots()
        {
            foreach (ITimePeriode day in m_TimePeriods.Values)
                foreach (ITimePeriode slot in day as WorkDay)
                    yield return slot as TimeSlot;
        }

        public void GetTimePeriode(DateTime StartTime, DateTime EndTime, ref ITimePeriode TimePeriod)
        {
            foreach (var day in m_TimePeriods.Values)
                day.GetTimePeriode(StartTime, EndTime, ref TimePeriod);

        }

        public void GetWorkingDates(ref List<DateTime> WorkingDates)
        {
            foreach (var day in m_TimePeriods.Values)
                day.GetWorkingDates(ref WorkingDates);
        }

	    public bool DeleteTimeslots(ref List<int> RemovedIndexes)
	    {
			bool result = false;
			foreach (var day in m_TimePeriods.Values)
				result |= day.DeleteTimeslots(ref RemovedIndexes);
			return result;
	    }

	    public void DeleteTimePeriode(ITimePeriode Periode)
	    {
			WorkDay deleteDay = Periode as WorkDay;
			if (deleteDay != null)
			{
				if (m_TimePeriods.ContainsKey(deleteDay.WorkDate))
					m_TimePeriods.Remove(deleteDay.WorkDate);
			}
			else
			{
				foreach (var day in m_TimePeriods.Values)
					day.DeleteTimePeriode(Periode);
			}

	    }
		public void DeleteTimeSlot(int Idx)
		{
			foreach (var periode in m_TimePeriods.Values)
				periode.DeleteTimeSlot(Idx);
		}


	    #endregion

    }
}